﻿namespace TvUtil
{

	#region Using

	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Windows.Forms;
	using System.Drawing;
	using System.Xml.Serialization;

	#endregion Using

	/// <summary>
	/// Třída televizní show
	/// </summary>
	[Serializable]
	public class ProgramShow
	{

		#region Members

		private TimeSpan m_time;
		private string m_name;
		private bool m_notify;

		#endregion Members

		#region Constructor

		/// <summary>
		/// Bezparametrický konstruktor
		/// </summary>
		public ProgramShow()
		{
		}

		/// <summary>
		/// Konstruktor
		/// </summary>
		/// <param name="time">Časové razítko (převádí se na <see cref="TimeStamp"/>).</param>
		/// <param name="name">Název show</param>
		public ProgramShow(string time, string name)
		{
			this.m_time = TimeSpan.Parse(time.Replace('.',':'));
			this.m_name = name;
		}

		#endregion Constructor

		#region Methods

		/// <summary>
		/// Metoda vytvoří objekt <see cref="ListViewItem"/> podle údajů v tomto objektu
		/// </summary>
		/// <returns><see cref="ListViewItem"/></returns>
		public ListViewItem CreateListViewItem()
		{			
			ListViewItem lvi = new ListViewItem();
			ListViewItem.ListViewSubItem sub = new ListViewItem.ListViewSubItem(lvi, this.m_name);
			this.Colorize(lvi);
			string txt = this.m_time.ToString();
			lvi.Text = txt.Substring(0, txt.Length-3);
			lvi.SubItems.Add(sub);
			lvi.Checked = this.Notify;
			lvi.Tag = this;
			return lvi;
		}

		/// <summary>
		/// Metoda obarví <see cref="ListViewItem"/>
		/// </summary>
		/// <param name="lvi"><see cref="ListViewItem"/></param>
		private void Colorize(ListViewItem lvi)
		{
			TimeSpan morningStart = new TimeSpan(6, 0, 0);
			TimeSpan morningEnd = new TimeSpan(10, 0, 0);
			TimeSpan noonEnd = new TimeSpan(12, 0, 0);
			TimeSpan afterNoonEnd = new TimeSpan(18, 0, 0);
			TimeSpan nightEnd = new TimeSpan(23, 59, 59);
			TimeSpan midnightStart = new TimeSpan(0, 0, 0);

			// Doba mezi půlnocí a ránem
			if (this.m_time > midnightStart && this.m_time <= morningStart)
			{
				lvi.BackColor = Color.SteelBlue;
				lvi.ForeColor = Color.White;
			}
			// Doba mezi ránem a začátkem dopoledne
			if (this.m_time > morningStart && this.m_time <= morningEnd)
			{
				lvi.BackColor = Color.LightBlue;
				lvi.ForeColor = Color.Black;
			}
			// Dopoledne
			if (this.m_time > morningEnd && this.m_time <= noonEnd)
			{
				lvi.BackColor = Color.White;
				lvi.ForeColor = Color.Black;
			}
			// Odpoledne
			if (this.m_time > noonEnd && this.m_time <= afterNoonEnd)
			{
				lvi.BackColor = Color.Yellow;
				lvi.ForeColor = Color.Black;
			}
			// Večíř
			if (this.m_time > afterNoonEnd && this.m_time <= nightEnd)
			{
				lvi.BackColor = Color.Orange;
				lvi.ForeColor = Color.Black;
			}
		}

		#endregion Methods

		#region Properties

		/// <summary>
		/// Nastaví či získá název show
		/// </summary>
		[XmlAttribute]
		public string Name
		{
			get
			{
				return this.m_name;
			}
			set
			{
				this.m_name = value;
			}
		}

		/// <summary>
		/// Berlička; pro serializaci
		/// </summary>
		[XmlAttribute]
		public string TimeForSerialize
		{
			get
			{
				return this.m_time.ToString();
			}
			set
			{
				this.m_time = TimeSpan.Parse(value);
			}
		}

		/// <summary>
		/// Nastaví či získá čas, ve který show začíná
		/// </summary>
		[XmlIgnore]
		public TimeSpan Time
		{
			get
			{
				return this.m_time;
			}
			set
			{
				this.m_time = value;
			}
		}

		/// <summary>
		/// Nastaví či získá upozornění na začátek show
		/// </summary>
		[XmlAttribute]
		public bool Notify
		{
			get
			{
				return this.m_notify;
			}
			set
			{
				this.m_notify = value;
			}
		}

		#endregion Properties

	}
}
